package com.neu.canteen.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neu.canteen.domain.Food;
import com.neu.canteen.service.FoodService;
import com.neu.canteen.service.impl.FoodServiceImpl;
import com.neu.canteen.utils.JsonModel;
import com.neu.canteen.utils.JsonUtil;
import com.neu.canteen.utils.StringUtil;

public class FoodController extends HttpServlet{
	
	private FoodService foodService = new FoodServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String typeId = req.getParameter("typeId");
		if(StringUtil.isEmpty(typeId)){
			//TODO 
		}else{
			int type = Integer.parseInt(typeId);
			
			List<Food> foodList = foodService.findByTypeId(type);
			
			JsonModel<List<Food>> jsonModel = new JsonModel<>(); 
			
			jsonModel.setTotal(foodList.size());
			jsonModel.setRows(foodList);
			
			PrintWriter printWriter = resp.getWriter();
			
			printWriter.print(JsonUtil.toJSONString(jsonModel));
			
		}}
	
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 		
			
			
		
			
			
			String action = req.getParameter("action");
			JsonModel jsonModel = new JsonModel();
	
			int result=-1;
			if("add".equals(action)){
				
				String name=req.getParameter("name");
				String price=req.getParameter("price");
				String packaging_rate=req.getParameter("packagingRate");
				String img=req.getParameter("img");
				String food_type_id=req.getParameter("foodTypeId");
				String remark=req.getParameter("remark");
				String material=req.getParameter("material");
				String status=req.getParameter("status");
				int Price=Integer.parseInt(price);
				int PackagingRate=Integer.parseInt(packaging_rate);
				int FoodTypeId=Integer.parseInt(food_type_id);
				int Status=Integer.parseInt(status);

				Food food = new Food();
				food.setName(name);
				food.setPrice(Price);
				food.setPackagingRate(PackagingRate);
				food.setImg(img);
				food.setFoodTypeId(FoodTypeId);
				food.setRemark(remark);
				food.setMaterial(material);
				food.setStatus(Status);
				
				result =foodService.add(food);
			}
			else if("del".equals(action)){
				
				String id = req.getParameter("id");
				int dd=Integer.parseInt(id);
				Food Food=new Food();
				Food.setId(dd);
				result=foodService.deleteFoodId(dd);
			}
			else if ("update".equals(action)) {
				
				String name=req.getParameter("name");
				String price=req.getParameter("price");
				String packaging_rate=req.getParameter("packagingRate");
				String img=req.getParameter("img");
				String food_type_id=req.getParameter("foodTypeId");
				String remark=req.getParameter("remark");
				String material=req.getParameter("material");
				String status=req.getParameter("status");
				
				int Price=Integer.parseInt(price);
				int PackagingRate=Integer.parseInt(packaging_rate);
				int FoodTypeId=Integer.parseInt(food_type_id);
				int Status=Integer.parseInt(status);
				 
				String id = req.getParameter("id");
				int d=Integer.parseInt(id);
				 
				
				Food food = new Food();
				food.setId(d);
				food.setName(name);
				food.setPrice(Price);
				food.setPackagingRate(PackagingRate);
				food.setImg(img);
				food.setFoodTypeId(FoodTypeId);
				food.setRemark(remark);
				food.setMaterial(material);
				food.setStatus(Status);
				
				
				result=foodService.updataFood(food);
			}
			
			
			if(result==-1){
				jsonModel.setMsg("失败");
				jsonModel.setSuccess(false);
			}else{
				jsonModel.setMsg("成功");
				jsonModel.setSuccess(true);
			}
			resp.getWriter().print(JsonUtil.toJSONString(jsonModel));
			 
	}
}